package com.dc.bobaoge.project.v3.to.v2.business.core;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dc.bobaoge.base.entity.message.common.po.MessagePraiseUserHistory;
import com.dc.bobaoge.base.mapper.message.MessagePraiseUserHistoryMapper;
import com.dc.bobaoge.project.base.core.old.entity.OldCoreMessageLaud;
import com.dc.bobaoge.project.base.core.old.mapper.OldCoreMessageLaudMapper;
import com.own.business.sync.trigger.base.entity.SyncTrigger;
import com.own.business.sync.trigger.base.method.ResolveMethod;
import com.own.component.common.exception.BusinessSimpleException;
import com.own.entity.tuple.Tuple;
import com.own.entity.tuple.Tuple2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * MessagePraiseResolveMethod
 *
 * @author chenxueli
 * @date 2024-01-13 14:53:49
 */
@Slf4j
@Component
public class MessagePraiseResolveMethod implements ResolveMethod {

    @Resource
    private MessagePraiseUserHistoryMapper messagePraiseUserHistoryMapper;

    @Resource
    private OldCoreMessageLaudMapper oldCoreMessageLaudMapper;

    /**
     * 修改的处理方法
     *
     * @param trigger 日志信息
     */
    @Override
    public void update(SyncTrigger trigger) {
        var history = getHistory(trigger.getTargetId(), trigger.getSharding());
        if (history == null) {
            throw new BusinessSimpleException("未查询到点赞的信息");
        }
        // 查询用户在旧版上有没有点赞过
        var wrapper = new LambdaQueryWrapper<OldCoreMessageLaud>()
                .eq(OldCoreMessageLaud::getLaudUserId, history.getFkUserId())
                .eq(OldCoreMessageLaud::getLaudMsgId, history.getFkMessageId())
                .last("limit 1");
        var item = oldCoreMessageLaudMapper.selectOne(wrapper);
        // 判断原状态
        if (history.getIsDeleted() == 1 && item == null) {
            return;
        }
        // 取消点赞，旧版数据存在的情况
        if (history.getIsDeleted() == 1 && item != null) {
            oldCoreMessageLaudMapper.deleteById(item.getLaudId());
            return;
        }
        // 如果不存在的情况
        if (item == null) {
            var praise = new OldCoreMessageLaud();
            praise.setLaudCreateTime(history.getGmtCreate());
            praise.setLaudMsgId(history.getFkMessageId());
            praise.setLaudUserId(history.getFkUserId());
            oldCoreMessageLaudMapper.insert(praise);
        }
    }

    /**
     * 根据季度集群获取信息
     *
     * @param id       主键id
     * @param sharding 分片键
     * @return 信息
     */
    private MessagePraiseUserHistory getHistory(Long id, String sharding) {
        var shardingTuple = resolveSharding(sharding);
        // 查询正常未删除的数据
        var messageType = shardingTuple.value1();
        var messageQuarter = shardingTuple.value2();
        return messagePraiseUserHistoryMapper.selectOneWithoutLogicDelete(id, messageType, messageQuarter);
    }

    /**
     * 处理分片键
     *
     * @param sharding 分片键
     * @return 分片键
     */
    private Tuple2<String, String> resolveSharding(String sharding) {
        try {
            var array = sharding.split("_");
            return Tuple.of(array[0], array[1]);
        } catch (Exception e) {
            throw new BusinessSimpleException("分片键格式不正确");
        }
    }

    /**
     * 名称信息
     *
     * @return 名称
     */
    @Override
    public String name() {
        return "message_praise_user_history";
    }

}
